home *** CD-ROM | disk | FTP | other *** search
- // -------- pstrings.h
-
- #ifndef PSTRINGS_H
- #define PSTRINGS_H
-
- #include <iostream.h>
- #include <string.h>
-
- // ============================
- // BASIC-like pString Class
- // ============================
- class pString {
- char *sptr;
- int length;
- void putstr(char *s);
- public:
- // -------- construct a null string
- pString() { sptr = NULL; }
- // --- construct with char * initializer
- pString(char *s);
- // ------- copy constructor
- pString(pString& s);
- // -------- construct with a size and fill character
- pString(int len, char fill = 0);
- // ------- destructor
- ~pString() { delete sptr; }
- // ------ return the length of a string
- int Strlen() { return strlen(sptr); }
- int Length() { return length; }
- // ---- substring: right len chars
- pString right(int len);
- // ---- substring: left len chars
- pString left(int len);
- // ---- substring: middle len chars starting from where
- pString mid(int len, int where);
- int FindChar(unsigned char ch);
- // ---------- assignment
- pString& operator=(pString& s);
- // ---------- conversion to char *
- operator char *() { return sptr; }
- // --- concatenation operator (str1 + str2;)
- pString operator+(pString& s);
- // --- concatenation operator (str1 += str2;)
- void operator+=(pString& s) { *this = *this + s; }
- // ------- relational operators
- pBool operator==(pString& s)
- { return (pBool) (strcmp(sptr,s.sptr) == 0); }
- pBool operator!=(pString& s)
- { return (pBool) (strcmp(sptr,s.sptr) != 0); }
- pBool operator>(pString& s)
- { return (pBool) (strcmp(sptr,s.sptr) > 0); }
- pBool operator<(pString& s)
- { return (pBool) (strcmp(sptr,s.sptr) < 0); }
- pBool operator<=(pString& s)
- { return (pBool) (!(*this > s)); }
- pBool operator>=(pString& s)
- { return (pBool) (!(*this < s)); }
- // ------- subscript
- char& operator[](int n) { return sptr[n]; }
- // ------- stream I/O
- friend ostream& operator<< (ostream& os, pString& str);
- friend istream& operator>> (istream& is, pString& str);
- };
-
- #endif
-